package pr1.cromosoma;

public class CromosomaF3 extends Cromosoma<Double> {

	private static final double XMIN = -250;
	private static final double XMAX = 250;
    private final int longX;

    private CromosomaF3(int longX) {
		super(longX);
        this.longX = longX;
        setAptitud(evalua());
	}

    @Override
    public CromosomaF3 clone() {
        CromosomaF3 clon = new CromosomaF3(longX);
        clon.copia(this);
        return clon;
    }

    public void copia(CromosomaF3 otro) {
        assert(this.longX == otro.longX);
        super.copia(otro);
        setAptitud(evalua());
    }

	@Override
	public final double evalua() {
        final double x = getX();
        double radicando = Math.sqrt(Math.abs(x));
        return - Math.abs(x * Math.sin(radicando));
	}

	public double getX() {
		return XMIN + (XMAX - XMIN) * bin2dec(0, longX)
				/ (Math.pow(2, longX) - 1);
	}

    @Override
    public Double getFenotipo() {
        return getX();
    }

	public static class Factory implements CromosomaFactory {

		double precision;
		int longCromosoma;

		public double getPrecision() {
			return precision;
		}

		public void setPrecision(double precision) {
			this.precision = precision;
			longCromosoma = (int) Math.ceil(Math.log(1 + (XMAX - XMIN)
					/ precision)
					/ Math.log(2));
		}

		public Cromosoma generaCromosoma() {
			return new CromosomaF3(longCromosoma);
		}
		
		public String toString() {
			return "Cromosoma 3";
		}
	}

}
